﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ArchiveLink.GUI
{
    using System.Data;
    using System.Diagnostics;
    using System.IO;
    using System.Management;
    using System.ServiceModel;
    using ArchiveLink;
    using ArchiveLinkService;
    using Microsoft.WindowsAPICodePack.Shell;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, ArchiveLinkService.ILinkerCallback
    {
        public string SourcePath { get; set; }
        public string TargetPath { get; set; }
        private LinkerClient _linker;
        private ArchiveLinkService.LinkerClient Linker
        {
            get
            {
                if (_linker == null)
                {
                    InstanceContext context = new InstanceContext(this);
                    _linker = new ArchiveLinkService.LinkerClient(context);
                    _linker.Connect();
                    StatusMessage.Text = "OK";
                }
                else
                {
                    if (_linker.State == CommunicationState.Faulted)
                    {
                        StatusMessage.Text = "ArchiveLink Service is not available";
                        InstanceContext context = new InstanceContext(this);
                        _linker = new ArchiveLinkService.LinkerClient(context);
                        _linker.Connect();
                        StatusMessage.Text = "OK";
                    }
                }
                return _linker;
            }
        }

        private Configuration _config;

        public MainWindow()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                _config = Linker.GetConfig();
            }
            catch (EndpointNotFoundException)
            {
                
            }

            ConfigPanel.DataContext = _config;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                Linker.AddDrive(((DriveInfo)comboBox1.SelectedItem).Name);
            }
        }

        #region Implementation of ILinkerCallback

        public bool DriveChanged(string name, string driveLetter, bool isMirrored)
        {
            if (!isMirrored && _config.AskOnNewDrives)
            {
                var result = MessageBox.Show("Update " + name + " in " + driveLetter + " (new: " + !isMirrored + ")", "",
                                             MessageBoxButton.YesNo);

                return result == MessageBoxResult.Yes;
            }
            return isMirrored;
        }

        #endregion

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (_config != null)
            {
                Linker.Configure(_config);
            }
            _config = Linker.GetConfig();
            ConfigPanel.DataContext = _config;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            comboBox1.ItemsSource = DriveInfo.GetDrives();
            comboBox1.SelectedIndex = 0;
        }
    }
}
